package 网络编程项目.login;

import org.apache.commons.lang3.StringUtils;
import 网络编程项目.login.entity.User;
import 网络编程项目.login.utiles.ClientToSendFile;
import 网络编程项目.login.utiles.ClientUtil;
import 网络编程项目.login.utiles.ImageUtil;
import 网络编程项目.login.utiles.JsonUtil;
import 网络编程项目.pojo.Message;
import 网络编程项目.pojo.MessageType;
import 网络编程项目.util.PathUtils;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.nio.channels.SocketChannel;

public class Register {
  private JPanel register;
  private JLabel registerLabel;
  private JTextField useridField;
  private JTextField usernameField;
  private JTextField phoneField;
  private JTextField emailField;
  private JButton registerButton;
  private JButton backButton;
  private JLabel useridLabel;
  private JLabel usernameLabel;
  private JLabel passwordLabel;
  private JLabel phoneLabel;
  private JLabel emailLabel;
  private JPasswordField passwordField;
  private JPanel panel1;
  private JPanel panel2;
  private JPanel panel3;
  private JButton uploadImage;
  private JLabel image;
  public static JFrame frame = new JFrame("Register");
  private static ClientUtil clientUtil = new ClientUtil();
  public static SocketChannel socketChannel = null;
  private static String imagePath = PathUtils.imgDir +"/天气之子.jpg";

  public Register() {
    uploadImage.setBorderPainted(false);
    registerButton.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {

        User user = new User();
        user.setUserid(useridField.getText().length() == 0 ? null : useridField.getText());
        user.setUsername(usernameField.getText().length() == 0 ? null : usernameField.getText());
        user.setPassword(new String(passwordField.getPassword()).length() == 0 ? null : new String(passwordField.getPassword()));
        user.setEmail(emailField.getText().length() == 0 ? null : emailField.getText());
        user.setPhone(phoneField.getText().length() == 0 ? null : phoneField.getText());

        if (isIncompatibleUser(user)) {
          RegisterTip.main();
        } else {
          user.setImage(ClientToSendFile.sendFile(imagePath));
          Message message = new Message();
          message.setType(MessageType.REGISTER);
          message.setMsg(JsonUtil.UserToString(user));
          System.out.println(message);

          clientUtil.sendMessage(socketChannel, message);

        }
      }
    });

    backButton.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        frame.setVisible(false);
        Login.main(socketChannel);
      }
    });
    uploadImage.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.showDialog(image, "选择图片");
        File file = chooser.getSelectedFile();
        if (file != null) {
          imagePath = file.getPath();
          image.setIcon(ImageUtil.change(new ImageIcon(imagePath), 85));
        }
      }
    });
  }

  private boolean isIncompatibleUser(User user) {


    return StringUtils.isEmpty(user.getUserid()) ||
        StringUtils.isEmpty(user.getPassword()) ||
        StringUtils.isEmpty(user.getUsername()) ||
        StringUtils.isEmpty(user.getPhone());
  }

  public static void main(SocketChannel channel) {
    socketChannel=channel;
    frame.setContentPane(new Register().register);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setLocation(600, 350);
  }
}
